<?php
$loader->requireOnce('includes/LabAssignment/LabAssignmentProcessor.class.php');
$loader->requireOnce('includes/LabException.class.php');
$loader->requireOnce('controllers/C_LabResult.class.php');


/**
 * Controler for processing lab result.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_LabAssignment extends Controller {
	private $logger ;
	private $labAssignmentProcessor;
	function __construct($template_mod = "general") {
		parent::Controller();
		$this->logger = LoggerManager::getLogger('module.dept_laboratory.controllers.C_LabAssignment');
		$this->template_mod = $template_mod;
		$this->view->path="lab_assignment";
		$this->labAssignmentProcessor = new LabAssignmentProcessor($this);
	}
	/**
	 * Render assignment page.
	 * @return html
	 */
	public function renderAssignmentPage($ticketId, $idOfActionAfterCreating) {
		$this->logger->info('Start actionRenderAssignmentPage for ticketId:' . $ticketId);
		$this->assign('idOfActionAfterCreating',$idOfActionAfterCreating);
		$html = $this->labAssignmentProcessor->renderAssignmentPage($ticketId);
		$this->logger->info('Finish actionRenderAssignmentPage for ticketId:' . $ticketId);
		return $html;
	}
	/**
	 * Render assignment page.
	 * @return html
	 */
	public function renderAssignmentPage4VEdit($ticketId, $labAssignmentTicketId, $idOfActionAfterUpdating) {
		$this->logger->info('Start renderAssignmentPage4VEdit for ticketId:' . $ticketId);
		$this->assign('idOfActionAfterUpdating',$idOfActionAfterUpdating);
		$result = $this->labAssignmentProcessor->renderAssignmentPage4VEdit($ticketId, $labAssignmentTicketId);
		$this->logger->info('Finish renderAssignmentPage4VEdit for ticketId:' . $ticketId);
		return $result;
	}
	/**
	 * List lab assignment of givent ticketId.
	 * @param $ticketId
	 * @return array[n](lab_assignment_ticket_id, request_time, diagnosis,
	 * <br>
	 *  PE.name treatment_doctor_name, treatment_doctor_id, enum_status, enum_status_text)
	 */
	public function listAssignments($ticketId) {
		$this->logger->info('Start listAssignments for ticketId:' . $ticketId);
		$result = $this->labAssignmentProcessor->listAssignments($ticketId);
		$this->logger->info('Finish listAssignments for ticketId:' . $ticketId);
		return $result;
	}

	public function deleteAssignment($labAssignmentTicketId) {
		$this->logger->info('Start deleteAssignment [' . $labAssignmentTicketId . ']');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		try {
			$result['result'] = $this->labAssignmentProcessor->deleteAssignment($labAssignmentTicketId);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('deleteAssignment lab assignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('deleteAssignment lab assignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('deleteAssignment lab assignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish deleteAssignment [' . $labAssignmentTicketId . ']');
		return $result;
	}

	public function deleteAssignmentOfGivenTicketId($ticketId) {
		$this->logger->info('Start deleteAssignments of given ticket id [' . $ticketId . ']');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		try {
			$result['result'] = $this->labAssignmentProcessor->deleteAssignment($labAssignmentTicketId);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('deleteAssignments of given ticket id [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('deleteAssignments of given ticket id [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('deleteAssignments of given ticket id [' . $ticketId . '] fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish deleteAssignments of given ticket id [' . $ticketId . ']');
		return $result;
	}
	

	/**
	 * List lab services of selected group.
	 * @return Array(service_id,name,price,insurance_price,patient_price)
	 */
	public function actionListLabServicesOfSelectedGroupAjax() {
		$this->logger->info('Start actionListSelectedLabGroupAjax');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		$selectedGroupId = $_GET['labGroupId'];
		$ticketId = $_GET['ticketId'];
		$result['result'] =  $this->labAssignmentProcessor->listLabServicesOfSelectedGroup($selectedGroupId);
		$this->labAssignmentProcessor->decoratePriceForServices($ticketId, $result['result']);

		$this->logger->info('Finish actionListSelectedLabGroupAjax');
		return json_encode($result);
			
	}
	/**
	 * Create lab assignment.
	 * @return result of creating assignment.
	 */
	public function actionCreatedAssignmentAjax() {
		$this->logger->info('Start actionCreatedAssignmentAjax for ticket:' + $_GET['ticketId']);
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		try {
			$labAssignmentTicket = array('ticketId'=>$_GET['ticketId'],
            'requestedTime'=>$_GET['requestedTime'],
            'doctorId'=>$_GET['doctorId'],
            'diagnosis'=>$_GET['diagnosis'],
            'labAssignedItem'=>$_GET['labAssignedItem'],
            'labGroup'=>$_GET['labGroup']
			);
			$result['result'] = $this->labAssignmentProcessor->createAssignment($labAssignmentTicket);
		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('create lab assignment for ticket [' . $_GET['ticketId'] . '] fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('create lab assignment for ticket [' . $_GET['ticketId'] . '] fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('create lab assignment for ticket [' . $_GET['ticketId'] . '] fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionCreatedAssignmentAjax for ticket:' + $_GET['ticketId']);
		return json_encode($result);
			
	}
	/**
	 * Start an assignment, add it to lab queue.
	 * @param $labAssignmentTicketId
	 * @return unknown_type
	 */
	public function startAssignment($labAssignmentTicketId) {
		$this->logger->info('Start startAssignment [' . $labAssignmentTicketId . ']');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		try {
			$result['result'] = $this->labAssignmentProcessor->startAssignment($labAssignmentTicketId);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('startAssignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('startAssignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('startAssignment [' . $labAssignmentTicketId . '] fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish startAssignment [' . $labAssignmentTicketId . ']');
		return $result;

	}
	
	
    public function actionPrintAssignment($labAssignmentTicketId=0) {
        if (isset($_GET['labAssignmentTicketId'])) {
            $labAssignmentTicketId = $_GET['labAssignmentTicketId'];
        }    	
    	return $this->labAssignmentProcessor->renderLabAssignment4Print($labAssignmentTicketId);     
    }

}
?>